<?php

defined('IN_IA') or exit('Access Denied');
$table = 'dictionary';
$dos = array('display', 'post', 'list', 'del', 'query');
$do = !empty($_GPC['do']) && in_array($do, $dos) ? $do : 'list';

//$cates = pdo_fetchall("SELECT * FROM " . tablename($table) . "  WHERE parent =0  ",array(),'id');
if ($do == 'list') {
    $_W['page']['title'] = '管理机关代字信息 - 机关代字数据列表';
    $condition = 'WHERE parent=2';
    $title = $_GPC['title'];
    if (!empty($title)) {
        $_GET['title'] = $title;
        $condition .= " AND title like '%{$title}%'";
    } else {
        $condition .= "";
    }
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20; //ORDER BY createtime DESC
    $list = pdo_fetchall("SELECT * FROM " . tablename($table) . $condition .' order by disporder ' . '   LIMIT ' . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($table) . $condition);
    $pager = pagination($total, $pindex, $psize);
    template('sms/dict2_list');
}

if ($do == 'post') {
    $_W['page']['title'] = '管理机关代字信息 - 增加机关代字数据';
    $id = intval($_GPC['id']);
    if (checksubmit('submit')) {
        $data = array(
            'parent' => 2,
            'title' => trim($_GPC['title']),
            'disporder' => $_GPC['disporder'],
        );
        $result = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE title='{$data['title']}'" );

        if (empty($id)&&(empty($result))) {
            pdo_insert($table, $data);
        } else {
            pdo_update($table, $data, array('id' => $id));
        }
        if(!empty($result)&&(empty($id))){
            message('此机关代字已存在,插入失败！',url('finance/dict2/list'),'success');
        }
        message('数据更新成功！', url('finance/dict2/list'), 'success');
    }
    if (!empty($id)) {
        $row = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE   id = '{$id}'");
    }else{
        $row=array('disporder'=>0,'parent'=>1);
    }
    template('sms/dict2_post');
}
if ($do == 'del') {
    $id = intval($_GPC['id']);
    if ($id > 0) {
        $row = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE   id = '{$id}'");
        if($row['parent']>0){
            pdo_delete($table,array('id' => $id));
            message('数据删除成功！', url('finance/dict2/list'), 'success');
        }else{
            message('无效参数！', url('finance/dict2/list'), 'error');
        }
    }
}
// 查询短信列表
if($do == 'query'){
    $ids = empty($_GPC['msgids']) ? array() : $_GPC['msgids'];
    $msgs = pdo_fetchall("SELECT * FROM " . tablename($table) . " WHERE parent = :parent" ,array(':parent'=>'1'),'id');
    //$ids =explode( ',',$_GPC['ids']);
    if (is_array($ids) && count($ids) > 0) {
        foreach ($msgs as $k => $v) {
            if (in_array($k, $ids)) {
                unset($msgs[$k]);
            }
        }
    }

    template('sms/dict_query');
}